package com.example.usermanagementsystem.startupRunner;

import com.example.usermanagementsystem.domain.Employee;
import com.example.usermanagementsystem.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static java.util.concurrent.TimeUnit.SECONDS;
 

@Component
public class MyStartupRunner implements CommandLineRunner {

    @Autowired
    private EmployeeService employeeService;
 
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void run(String... args) throws Exception {
//        this.employeeMapper = BeanContext.getApplicationContext().getBean(EmployeeMapper.class);
        List<Employee> employeeList = redisTemplate.opsForList().range("unloadEmployeeList",0,-1);
        if (CollectionUtils.isEmpty(employeeList)){
            employeeList = employeeService.queryAll();
            for (Employee e : employeeList) {
                redisTemplate.opsForList().leftPush("unloadEmployeeList",e );
            }
            redisTemplate.expire("unloadEmployeeList",100,SECONDS);
        }
    }
}